package util;

import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;



public class WebConnector{

	public Log log = LogFactory.getLog("GeneralLogger");
	public Log errorLog = LogFactory.getLog("ErrorLogger");
	public Properties OR = null;
	public Properties CONFIG = null;
	public Properties BROWSER = null;
	WebDriver driver = null;
	

	static WebConnector w;

	/************** Logging ***************/
	public void log(String msg) {
		log.debug(msg);
	}

	/******** Singleton 
	 * @throws Exception **********/
	public static WebConnector getInstance(){
		try {
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (w == null)
			w = new WebConnector();
		
		return w;
	}

	public WebConnector() {
		log("load");
		if (OR == null) {
			try {
				// initialize OR
				OR = new Properties();
				OR.load(this.getClass().getClassLoader()
						.getResourceAsStream("OR.properties"));

			} catch (Exception e) {
				System.out.println("Error on intializing properties files:"
						+ e.getCause());
				e.printStackTrace();
			}

			if (CONFIG == null) {
				try {

					// initialize CONFIG to corresponding env
					CONFIG = new Properties();
					CONFIG.load(this.getClass().getClassLoader()
							.getResourceAsStream("config.properties"));

				} catch (Exception e) {
					System.out.println("Error on intializing properties files:"
							+ e.getCause());
					e.printStackTrace();
				}
			}

		}
	}

	public synchronized WebDriver SharedDriver(){
		log("Opening Driver ");
			
		try {

			if (driver == null) {
				DesiredCapabilities cap = null;

				if (System.getProperty("browser").equalsIgnoreCase("firefox")) {
					FirefoxProfile profile = new FirefoxProfile();
					profile.setAcceptUntrustedCertificates(true);
					cap = DesiredCapabilities.firefox();
					cap.setBrowserName("firefox");
					System.out.println(cap.getBrowserName());
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("WIN7HubURL")), cap);
				} else if (System.getProperty("browser").equalsIgnoreCase(
						"chrome")) {
					cap = DesiredCapabilities.chrome();
					cap.setBrowserName("chrome");

					cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
					System.out.println(cap.getBrowserName());
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("WIN7HubURL")), cap);
				} else if (System.getProperty("browser").equalsIgnoreCase("ie")) {
					cap = DesiredCapabilities.internetExplorer();
					cap.setBrowserName("internet explorer");
					cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
					cap.setCapability(
							InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
							true);
					cap.setCapability("ensureCleanSession", true);
					System.out.println(cap.getBrowserName());
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("WIN7HubURL")), cap);
					// to handle https
					// driver.navigate().to("javascript:document.getElementbyID('overridelink').click()");
				} 
                 //xp
				else if (System.getProperty("browser").equalsIgnoreCase("firefox")) {
					FirefoxProfile profile = new FirefoxProfile();
					profile.setAcceptUntrustedCertificates(true);
					cap = DesiredCapabilities.firefox();
					cap.setBrowserName("firefox");
					cap.setPlatform(org.openqa.selenium.Platform.XP);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("xphubUrl")), cap);
					driver.manage().window().maximize();
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
			
				else if (System.getProperty("browser").equalsIgnoreCase("ie")) {
					cap = DesiredCapabilities.internetExplorer();
					cap.setBrowserName("internet explorer");
					cap.setPlatform(org.openqa.selenium.Platform.XP);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("xphubUrl")), cap);
					driver.manage().window().maximize();
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// Chrome
				else if (System.getProperty("browser").equalsIgnoreCase("chrome")) {
					cap = DesiredCapabilities.chrome();
					cap.setBrowserName("chrome");
					cap.setPlatform(org.openqa.selenium.Platform.XP);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("xphubUrl")), cap);
					driver.manage().window().maximize();
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// OS Vista
				// Firefox
				else if (System.getProperty("browser").equalsIgnoreCase("firefox")) {
					FirefoxProfile profile = new FirefoxProfile();
					profile.setAcceptUntrustedCertificates(true);
					cap = DesiredCapabilities.firefox();
					cap.setBrowserName("firefox");
					cap.setPlatform(org.openqa.selenium.Platform.VISTA);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("VistahubUrl")), cap);
					driver.manage().window().maximize();
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// IE
				else if (System.getProperty("browser").equalsIgnoreCase("ie")) {
					cap = DesiredCapabilities.internetExplorer();
					cap.setBrowserName("internet explorer");
					cap.setPlatform(org.openqa.selenium.Platform.VISTA);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("VistahubUrl")), cap);
					driver.manage().window().maximize();
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// Chrome
				else if (System.getProperty("browser").equalsIgnoreCase("chrome")) {
					cap = DesiredCapabilities.chrome();
					cap.setBrowserName("chrome");
					cap.setPlatform(org.openqa.selenium.Platform.VISTA);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("VistahubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// OS MAC
				// Firefox
				else if (System.getProperty("browser").equalsIgnoreCase("firefox")) {
					cap = DesiredCapabilities.firefox();
					cap.setBrowserName("firefox");
					cap.setPlatform(org.openqa.selenium.Platform.MAC);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("MAChubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				
				else if (System.getProperty("browser").equalsIgnoreCase("ie")) {
					cap = DesiredCapabilities.internetExplorer();
					cap.setBrowserName("internet explorer");
					cap.setPlatform(org.openqa.selenium.Platform.MAC);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("MAChubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				
				else if (System.getProperty("browser").equalsIgnoreCase("chrome")) {
					cap = DesiredCapabilities.chrome();
					cap.setBrowserName("chrome");
					cap.setPlatform(org.openqa.selenium.Platform.MAC);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("MAChubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// OS Linux
				// Firefox
				else if (System.getProperty("browser").equalsIgnoreCase("firefox")) {
					FirefoxProfile profile = new FirefoxProfile();
					profile.setAcceptUntrustedCertificates(true);
					cap = DesiredCapabilities.firefox();
					cap.setBrowserName("firefox");
					cap.setPlatform(org.openqa.selenium.Platform.LINUX);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("LinuxhubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// IE
				else if (System.getProperty("browser").equalsIgnoreCase("ie")) {
					cap = DesiredCapabilities.internetExplorer();
					cap.setBrowserName("internet explorer");
					cap.setPlatform(org.openqa.selenium.Platform.LINUX);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("LinuxhubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				}
				// Chrome
				else if (System.getProperty("browser").equalsIgnoreCase("chrome")) {
					cap = DesiredCapabilities.chrome();
					cap.setBrowserName("chrome");
					cap.setPlatform(org.openqa.selenium.Platform.LINUX);
					driver = new RemoteWebDriver(new URL(
							CONFIG.getProperty("LinuxhubUrl")), cap);
					driver.manage().timeouts()
							.implicitlyWait(30, TimeUnit.SECONDS);
				} else if (System.getProperty("browser").equalsIgnoreCase(
						"ipad")) {
					cap = DesiredCapabilities.ipad();
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new IPhoneDriver(CONFIG.getProperty("IPadHubURL"));
				} else if (System.getProperty("browser").equalsIgnoreCase(
						"iphone")) {
					cap = DesiredCapabilities.iphone();
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new IPhoneDriver(
							CONFIG.getProperty("IPhoneHubURL"));
				} else if (System.getProperty("browser").equalsIgnoreCase(
						"android")) {
					cap = DesiredCapabilities.android();
					cap.setPlatform(org.openqa.selenium.Platform.ANY);
					driver = new AndroidDriver();
				}
				else {
					// firefox - no GRID
					driver = new FirefoxDriver();
				}
	
				driver = driver;
			}

			driver.manage().window().maximize();

			driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error on intializing Browser");
		}
		return driver;
	}
	
	

}
